/* 
 * Copyright (c) 2022, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.admin.sys.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cory.admin.sys.controller.AdminController;
import com.cory.admin.sys.domain.entity.SysDept;
import com.cory.admin.sys.service.SysDeptService;
import com.cory.boot.domain.bean.Result;
import com.cory.boot.domain.enumdef.EState;
import com.cory.boot.utils.SysUtils;
import com.cory.utils.lang.StringUtils;

/**
 * @author xiongcong
 * @date 2022-01-14
 */
@Controller
@RequestMapping("admin/sys/dept")
public class SysDeptController extends AdminController {
    @Autowired
    private SysDeptService sysDeptService;
    
    @RequestMapping("list")
    public String list(SysDept param, Model model) {
        param.setTenantId(super.getTenantId());
        param.setUserType(super.getPrincipal().getUserType());
        model.addAttribute("list", sysDeptService.sortList(param));
        return "admin/sys/dept/list";
    }
    
    @RequestMapping("form")
    public String form(Integer id, String parent, Model model) {
        if (id != null) {
            SysDept info = sysDeptService.getById(id);
            parent = info.getParent();
            model.addAttribute("info", info);
        }
        if (StringUtils.isNotEmpty(parent) && !"0".equals(parent)) {
            model.addAttribute("parentInfo", sysDeptService.getByCode(super.getTenantId(), parent));
        }
        if ("0".equals(parent)) {
            SysDept tmp = new SysDept();
            tmp.setCode("0");
            tmp.setName(SysUtils.getEntName());
            model.addAttribute("parentInfo", tmp);
        }
        model.addAttribute("parent", parent);
        
        return "admin/sys/dept/form";
    }
    
    @RequestMapping("save")
    @ResponseBody
    public Result<?> save(SysDept info) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.ne("state", EState.DELETE.key).eq("name", info.getName()).eq("tenant_id", super.getTenantId());
        
        SysDept tmp = sysDeptService.getOne(wrapper);
        if (tmp != null && (info.getId() == null || !info.getId().equals(tmp.getId()))) {
            return Result.fail("部门已存在");
        }
        
        if (info.getId() == null) {
            info.setTenantId(super.getTenantId());
            info.setUserType(super.getPrincipal().getUserType());
            sysDeptService.save(info);
        } else {
            sysDeptService.updateById(info);
        }
        
        return Result.success("保存成功");
    }
    
    @RequestMapping("delete")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(Integer id) {
        sysDeptService.delete(id);
        return Result.success("删除成功");
    }
    
    @RequestMapping("selectTree")
    @ResponseBody
    public Result<?> selectTree() {
        return Result.success("ok", sysDeptService.selectTree(super.getTenantId(), super.getPrincipal().getUserType()));
    }
}
